﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using RapidWebDev.SearchIndex.Contract;

namespace RapidWebDev.SearchIndex.DocumentFields
{
	/// <summary>
	/// Interface to access indexing document field.
	/// </summary>
	internal class KeywordDocumentField : BaseDocumentField
	{
		/// <summary>
		/// Indexing document field type.
		/// </summary>
		public override IndexDocFieldValueTypes FieldType { get { return IndexDocFieldValueTypes.Keyword; } }

		/// <summary>
		/// Convert indexing document field message to lucene fields.
		/// </summary>
		/// <param name="indexDocField"></param>
		/// <returns></returns>
		public override Fieldable[] GetLuceneFields(IndexDocFieldMessage indexDocField)
		{
			if (indexDocField.Values == null || indexDocField.Values.Count == 0)
				return new Field[0];

			List<Fieldable> fields = new List<Fieldable> 
			{
				new Field(LuceneFields.FieldType(indexDocField.Name), this.FieldType.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)
			};

			foreach (string fieldValue in indexDocField.Values)
			{
				fields.Add(new Field(indexDocField.Name, fieldValue, Field.Store.YES, Field.Index.NOT_ANALYZED));
				if(indexDocField.Sortable)
					fields.Add(new Field(LuceneFields.SortableFieldName(indexDocField.Name), fieldValue, Field.Store.YES, Field.Index.NOT_ANALYZED));
			}

			return fields.ToArray();
		}

		/// <summary>
		/// Resolve original field value from Lucene stored field value.
		/// </summary>
		/// <param name="luceneStoredFieldValue"></param>
		/// <returns></returns>
		public override string ResolveOriginalFieldValue(string luceneStoredFieldValue)
		{
			return luceneStoredFieldValue;
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected override Query BuildAnyQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count == 0) return null;
			BooleanQuery queryToRun = new BooleanQuery();
			foreach (string filterValue in searchFilterMessage.Values)
			{
				if (string.IsNullOrEmpty(filterValue)) continue;
				Term filterValueTerm = new Term(searchFilterMessage.FieldName, filterValue);
				queryToRun.Add(new TermQuery(filterValueTerm), BooleanClause.Occur.SHOULD);
			}

			return queryToRun;
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected override Query BuildAllQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count == 0) return null;
			BooleanQuery queryToRun = new BooleanQuery();
			foreach (string filterValue in searchFilterMessage.Values)
			{
				if (string.IsNullOrEmpty(filterValue)) continue;
				Term filterValueTerm = new Term(searchFilterMessage.FieldName, filterValue);
				queryToRun.Add(new TermQuery(filterValueTerm), BooleanClause.Occur.MUST);
			}

			return queryToRun;
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected override Query BuildExcludeQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count == 0) return null;
			BooleanQuery queryToRun = new BooleanQuery();
			foreach (string filterValue in searchFilterMessage.Values)
			{
				if (string.IsNullOrEmpty(filterValue)) continue;
				Term filterValueTerm = new Term(searchFilterMessage.FieldName, filterValue);
				queryToRun.Add(new TermQuery(filterValueTerm), BooleanClause.Occur.SHOULD);
			}

			return queryToRun;
		}

		private static string FilterValue(string filterValue)
		{
			if (string.IsNullOrEmpty(filterValue)) return null;

			if (filterValue[0] == '*' || filterValue[0] == '?')
			{
				if (filterValue.Length == 1) return null;
				return FilterValue(filterValue.Substring(0));
			}

			return filterValue;
		}
	}
}
